from zope.formlib.form import FormFields
from plone.app.form.widgets import DisabledCheckBoxWidget

from zope.interface import Interface
from zope.component import adapts
from zope.interface import implements
from zope.component import getUtility

from Acquisition import aq_inner

from Products.CMFCore.utils import getToolByName
from Products.CMFDefault.formlib.schema import ProxyFieldProperty
from Products.CMFDefault.formlib.schema import SchemaAdapterBase
from Products.CMFPlone import PloneMessageFactory as _
from Products.CMFCore.interfaces import ISiteRoot, IPropertiesTool

from plone.app.controlpanel.form import ControlPanelForm
from plone.app.controlpanel.widgets import LanguageTableWidget
from plone.app.controlpanel.widgets import AllowedTypesWidget

from plone.app.form.widgets import LanguageDropdownChoiceWidget

from plone.dexterity.interfaces import IDexterityFTI
 
from plone.app.multilingual.interfaces import ITranslationUtility, IPolicyOptions, IMLangOptions, ILangOptions
from plone.app.controlpanel.language import ILanguageSelectionSchema

from plone.fieldsets.fieldsets import FormFieldsets

from plone.app.controlpanel.language import LanguageControlPanelAdapter, LanguageControlPanel

MARKER = 'plone.app.multilingual.interfaces.ITranslatable'

class MultiLingualControlPanelAdapter(SchemaAdapterBase):
    
    adapts(ISiteRoot)
    implements(ILangOptions)

    def __init__(self, context):
        super(MultiLingualControlPanelAdapter, self).__init__(context)
        self.ptool = getUtility(IPropertiesTool).multilingual_properties
        self.context = getToolByName(context, 'portal_languages')
    
    
    #
    # From language control panel
    # 

    def get_default_language(self):
        return aq_inner(self.context).getDefaultLanguage()

    def set_default_language(self, value):
        context = aq_inner(self.context)
        if isinstance(value, tuple):
            value = value[0]
        supported_langs = context.getSupportedLanguages()
        if value not in supported_langs:
            context.supported_langs = [value]
        context.setDefaultLanguage(value)

    default_language = property(get_default_language,
                                set_default_language)

    def get_use_combined_language_codes(self):
        return aq_inner(self.context).use_combined_language_codes

    def set_use_combined_language_codes(self, value):
        context = aq_inner(self.context)
        # We are disabling the combined codes, but still have one selected
        # as the default.
        default = context.getDefaultLanguage()
        if len(default.split('-')) > 1:
            # XXX This should be done in some kind of validate method instead,
            # but I have no time to figure out that part of formlib right now
            request = context.REQUEST
            message = _(u"You cannot disable country-specific language "
                         "variants, please choose a different site "
                         "language first.")
            IStatusMessage(request).addStatusMessage(message, type='error')
        else:
            context.use_combined_language_codes = value

    use_combined_language_codes = property(get_use_combined_language_codes,
                                           set_use_combined_language_codes)


 
    # This property is stored at portal_languages
    supported_langs = ProxyFieldProperty(ILangOptions['supported_langs'])
    display_flags = ProxyFieldProperty(ILangOptions['display_flags'])
    use_path_negotiation = ProxyFieldProperty(ILangOptions['use_path_negotiation'])
    use_cookie_negotiation = ProxyFieldProperty(ILangOptions['use_cookie_negotiation'])
    use_cctld_negotiation = ProxyFieldProperty(ILangOptions['use_cctld_negotiation'])
    use_request_negotiation = ProxyFieldProperty(ILangOptions['use_request_negotiation'])
    use_default_language = ProxyFieldProperty(ILangOptions['use_default_language'])
    force_language_urls = ProxyFieldProperty(ILangOptions['force_language_urls'])
    start_neutral = ProxyFieldProperty(ILangOptions['start_neutral'])
    
    # This property is at propertysheet
    def get_supported_types(self):
        return self.ptool.supported_types
    def set_supported_types(self,values):
        self.ptool.supported_types=values
                
    supported_types = property(get_supported_types,
                               set_supported_types)
    
    
    

        
policy = FormFieldsets(IPolicyOptions)
policy.id = 'multilingualpolicy'
policy.label = _(u'label_multilingual_policy', default=u'Multilingual policy')

languageoptions = FormFieldsets(IMLangOptions)
languageoptions.id = 'multilinguallangoptions'
languageoptions.label = _(u"heading_multilingual_settings", default="Multilingual Settings")

languagesettings = FormFieldsets(ILanguageSelectionSchema)
languagesettings.id = 'languagesettings'
languagesettings.label = _(u"heading_language_settings", default="Language Settings")

class MultiLingualControlPanel(ControlPanelForm):
    form_fields = FormFieldsets(languagesettings, languageoptions, policy)
    
    #form_fields['supported_langs'].custom_widget = LanguageTableWidget
    form_fields['supported_types'].custom_widget = AllowedTypesWidget
    form_fields['default_language'].custom_widget = LanguageDropdownChoiceWidget
    label = _(u"heading_language_settings", default="Language Settings")
    description = _(u"description_language_settings",
                    default="Settings related to "
                            "multilingual content.")
    form_name = _(u"heading_language_settings", default="Language Settings")
 


